package java6681.task;

/**
 * @Title: Vehicle6681
 * @Author Junichiro
 * @Description:TODO
 * @Date: 2020/12/17 0017 下午 7:14
 */
  abstract class Vehicle6681 {
    private String name;
    private String type;
    private double speed;

    public Vehicle6681() {
    }

    public Vehicle6681(String name, String type, double speed) {
        this.name = name;
        this.type = type;
        this.speed = speed;
        setSpeed(speed);

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        if (speed < 0){
            throw new VehicleException6681("speed不能是负数");
        }
        this.speed = speed;
    }

    public abstract void run() ;

    @Override
    public String toString() {
        return name + "," + speed + "公里/时";
    }

}

class Bus6681 extends Vehicle6681 {
    private int wheels;

    public Bus6681() {
        super();
        super.setType("公共汽车");
    }

    public Bus6681(String name, String type, double speed, int wheels) {
        super(name, type, speed);
        this.wheels = wheels;
        setWheels(wheels);
    }

    public int getWheels() {
        return wheels;
    }

    public void setWheels(int wheels) {
        if (!(wheels>4||wheels==4)){
            throw new VehicleException6681("wheels必须大于或等于4");
        }
        this.wheels = wheels;
    }

    @Override
    public void run() {
        System.out.println("司机手握方向盘开车……");
    }

    @Override
    public String toString() {
        return super.toString() + "," + getWheels() + "轮";
    }
}

class Ship6681 extends Vehicle6681 {
    private int displacement;

    public Ship6681() {
        super();
        super.setType("轮船");
    }

    public Ship6681(String name, String type, double speed, int displacement) {
        super(name, type, speed);
        this.displacement = displacement;
        setDisplacement(displacement);
    }

    public int getDisplacement() {
        return displacement;
    }

    public void setDisplacement(int displacement)  {
        if (displacement < 0){
            throw new VehicleException6681("排水量不能是负数");
        }
            this.displacement = displacement;
    }

    @Override
    public void run() {
        System.out.println("船长站着掌舵……");
    }

    @Override
    public String toString() {
        return super.toString() + ",排水量" + getDisplacement() + "吨";
    }
}

class Plane6681 extends Vehicle6681 {
    public Plane6681() {
        super();
        super.setType("飞机");
    }

    public Plane6681(String name, String type, double speed) {
        super(name, type, speed);
    }

    @Override
    public void run() {
        System.out.println("飞行员手握操纵杆飞行……");
    }
}

public class VehicleDemo6681 {
    public static void main(String[] args) {
        Vehicle6681[] vehicle6681s = new Vehicle6681[]{
                new Plane6681("MF8115航班", "波音747", 800.0),
                new Ship6681("闽江#1", "轮船", 30.0, 1000),
                new Plane6681("CZ5111航班", "波音787-9", 850.0),
                new Bus6681("16路公交车", "金龙", 25.6, 4),
                new Bus6681("101路公交车", "宇通", 38.7, 4)};
        VehicleDemo6681 vehicleDemo6681 = new VehicleDemo6681();
        vehicleDemo6681.runAll6681(vehicle6681s);
    }

    void runAll6681(Vehicle6681[] vehicle6681s) {
        for (int i = 0; i < vehicle6681s.length; i++) {
            System.out.println("交通工具" + (i + 1) + ":" + vehicle6681s[i].getType());
            System.out.println(vehicle6681s[i]);
            vehicle6681s[i].run();
            System.out.println("");
        }
    }
}

